(library (statistics)
  (export do-n-times
          count-things)
  (import (except (rnrs base) error map)
          (only (guile)
                lambda* λ)
          ;; SRFI 1: list procedures
          ;; (srfi srfi-1)
          ;; SRFI 69: hash tables
          (srfi srfi-69))


  (define do-n-times
    (λ (proc n)
      (let loop ([n n] [results '()])
        (cond
         [(> n 0)
          (loop (- n 1) (cons (proc) results))]
         [else results]))))


  (define count-things
    (λ (things thing->hash-key count-proc init-count)
      (let ([count-table (make-hash-table)])
        (for-each
         (λ (thing)
           (let ([hash-key (thing->hash-key thing)])
             (hash-table-set!
              count-table
              hash-key
              (+ (hash-table-ref count-table
                                 hash-key
                                 (λ () init-count))
                 (count-proc thing)))))
         things)
        count-table))))
